package cn.bdqn.service.impl;

import cn.bdqn.domain.Client;
import cn.bdqn.domain.Replenish;
import cn.bdqn.domain.ReplenishEquip;
import cn.bdqn.domain.SendFreight;
import cn.bdqn.mapper.ClientMapper;
import cn.bdqn.mapper.ReplenishEquipMapper;
import cn.bdqn.mapper.ReplenishMapper;
import cn.bdqn.mapper.SendFreightMapper;
import cn.bdqn.service.ReplenishService;
import cn.bdqn.utils.result.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 补单业务实现类
 * @author dkj
 */
@Service
public class ReplenishServiceImpl implements ReplenishService {

    @Autowired
    private ReplenishMapper replenishMapper;

    @Autowired
    private SendFreightMapper sendFreightMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private ReplenishEquipMapper replenishEquipMapper;
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Replenish queryByPrimaryKey(Integer id) {
        //根据id查询
        Replenish replenish = replenishMapper.selectByPrimaryKey(id);
        //根据补货单id查询
        List<ReplenishEquip> replenishEquips = replenishEquipMapper.selectByReplenishId(replenish.getId());
        //赋值
        replenish.setReplenishEquips(replenishEquips);
        //返回结果
        return replenish;
}

    /**、
     *根据发货单号和补货单状态查询  模糊  分页
     * @param currentPage 当前页
     * @param pageSize 显示数量
     * @param sendFreightNumber  发货单号
     * @param transportState 补货单状态
     * @return
     */
    @Override
    public Page queryBySendNumberAndTransportState(Integer currentPage, Integer pageSize, String sendFreightNumber, Integer transportState) {

        //开启分页
        PageHelper.startPage(currentPage>0?currentPage:1,pageSize);
        //根据发货单号模糊查询
        List<SendFreight> sendFreights = sendFreightMapper.selectByLikeSendFreightNumber(sendFreightNumber);
        //根据发货单号和补货单状态查询
        List<Replenish> replenishes = replenishMapper.selectBySendNumberAndTransportState(sendFreights, transportState);
        //分页
        PageInfo<Replenish> pageInfo = new PageInfo<>(replenishes);
        //封装
        Page page = new Page();
        //list集合
        page.setMenuList(pageInfo.getList());
        //设置总数量
        page.setTotalRecord((int)pageInfo.getTotal());
        //返回结果
        return page;
    }


    /**
     * 新增
     * @param replenish
     */
    @Override
    public Replenish saveReplenish(Replenish replenish) {

        //发货单对象
        SendFreight sendFreight = sendFreightMapper.selectByPrimaryKey(replenish.getSendFreightId());
        //设置发货对象
        replenish.setSendFreight(sendFreight);
        //客户对象
        Client client = clientMapper.selectByPrimaryKey(replenish.getClientId());
        //设置客户对象
        replenish.setClient(client);
        //添加
        replenishMapper.insertReplenish(replenish);
        //返回结果
        return replenish;

    }

    /**
     * 修改
     * @param replenish
     */
    @Override
    public void updateReplenish(Replenish replenish) {

        //修改
        replenishMapper.updateReplenish(replenish);
    }
}
